package com.sll.myzhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sll.myzhxy.pojo.Admin;
import com.sll.myzhxy.service.AdminService;
import com.sll.myzhxy.util.MD5;
import com.sll.myzhxy.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiao_an
 * @create 2022-05-19  17:15
 */

@RestController//@ResponseBody + @Controller
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("删除单个或多个管理员信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(
            @ApiParam("要删除的管理员编号的JSON数组") @RequestBody List<Integer> ids
    ) {
        adminService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("保存或者修改管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(
            @ApiParam("要保存或修改的管理员JSON") @RequestBody Admin admin
    ) {
        Integer id = admin.getId();
        if (null == id || 0 == id) {
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    @ApiOperation("分页带条件查询管理员信息")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @ApiParam("分页查询的页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("分页查询的页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询的查询条件") String adminName) {
        //分页信息封装Page对象
        Page<Admin> page = new Page<>(pageNo, pageSize);
        //进行查询
        IPage<Admin> iPage = adminService.getAdminsByOpr(page, adminName);
        //封装Result返回
        return Result.ok(iPage);
    }
}
